package models.admin;

import java.util.Collections;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;

import play.db.jpa.Model;

/**
 * @author rjanixz
 *
 */
@Entity
@Audited
public class User extends Model {

	@Column(unique=true, nullable=false)
	private String email;
	@Column(nullable=false)
	private String password;
	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name="RoleUser",
			joinColumns={@JoinColumn(name="roleId",referencedColumnName="id")},
			inverseJoinColumns={@JoinColumn(name="userId",referencedColumnName="id")},
			uniqueConstraints = {@UniqueConstraint(columnNames = {"roleId","userId"})})
	@Audited(targetAuditMode=RelationTargetAuditMode.AUDITED)
	private List<Role> roles;
	
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}

	public List<Role> getRoles() {
		if (roles == null) {
			roles = Collections.<Role>emptyList();
		}
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
}
